package com.hxf.dao.menu.impl;
 
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dao.BaseDaoImpl;
import com.hxf.dao.menu.MenuDao; 
import com.hxf.dto.MenuDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.RoleMenuDto;
import com.hxf.exception.DaoLevelException;
import com.hxf.web.vo.MenuVo;

/**
 * DAO菜单实现类
 * @author tengyicheng
 * @version [1.0, 2013/09/23]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@SuppressWarnings("unchecked")
@Repository("menuDao")
public class MenuDaoImpl extends BaseDaoImpl implements MenuDao {

	@Override
	public ReturnData<MenuDto> findByMenu(MenuVo menuDto, int targetPage,
			int pageSize) throws DaoLevelException {
		String sql = "";
		if (PublicUtil.isNotNull(menuDto) && PublicUtil.isNotNullAndEmpty(menuDto.getMenuName())) {
			sql = " and menu_name like '"+menuDto.getMenuName().trim()+"%'";
		}
		
		MenuDto menu = null;
		
		List<MenuDto> menuList =  new ArrayList<MenuDto>();
		try{
			ReturnData data = super.exeSQLList("SELECT * from rk_dic_menu AS m LEFT JOIN rk_role_menu AS r ON r.menu_id = m.menu_id WHERE 1=1 and role_id="+menuDto.getRoleId() +sql,targetPage,pageSize);
 
			List list = data.getResultlist();
		
			if(list!=null || 0 == list.size()){
				Object[] o = null;
				for(int i=0;i<list.size();i++){
					o = (Object[])list.get(i);
					menu = new MenuDto();
					menu.setMenuId(o[0]==null?null:Integer.valueOf(o[0].toString()));
					menu.setCreateTime(o[1]==null?null:o[1].toString());
					menu.setCreateUser(o[2]==null?null:o[2].toString());
					menu.setMenuName(o[3]==null?null:o[3].toString());
					menu.setMenuOrder(o[4]==null?null:Integer.valueOf(o[4].toString()));
					menu.setMenuUrl(o[5]==null?null:o[5].toString());
					menu.setModifyDate(o[6]==null?null:o[6].toString());
					menu.setModifyUser(o[7]==null?null:o[7].toString());
					menu.setParentId(o[8]==null?null:Integer.valueOf(o[8].toString()));
					menu.setStatus(o[9]==null?null:Integer.valueOf(o[9].toString()));
					menuList.add(menu);
					
				}
			}
		    data.setResultlist(menuList);
			return data;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		
		//return super.findBy("from MenuDto WHERE 1=1 "
		//		+ sql, targetPage, pageSize); 
	}
	
	@Override
	public List<MenuDto> findByMenu(MenuDto menuDto) throws DaoLevelException {
		String sql = "";
		if (PublicUtil.isNotNull(menuDto) && PublicUtil.isNotNullAndEmpty(menuDto.getMenuName())) {
			sql = " and menuName like '"+menuDto.getMenuName()+"%'";
		}
		return super.findBy("from MenuDto WHERE 1=1" + sql + " ORDER BY menuOrder"); 
	} 

	/**
	 * 联盟管理员查询
	 */
	@Override
	public List<MenuDto> findMenuTree(Integer userId) throws DaoLevelException {
		String sql = "select distinct dm.menu_id,dm.menu_name,dm.menu_order,dm.parent_id,dm.menu_url" +
				" from rk_admin_user u,rk_role r,rk_admin_user_role ur,rk_dic_menu dm,rk_role_menu rm" +
						" where u.id=ur.user_id" 
						+" and r.role_id=ur.role_id"
						+" and dm.menu_id = rm.menu_id"
						+" and r.role_id = rm.role_id"
						+" and dm.status = 0 and u.id="+ userId; //status=0 菜单状态为有效
		List list = super.exeSQLList(sql);
		List<MenuDto> menuList =  new ArrayList<MenuDto>();
		MenuDto menuDto = null;
		if (list != null && list.size() > 0) {
			for (int i = 0;i < list.size(); i++) {
				Object [] o = (Object[])list.get(i);
				menuDto = new MenuDto();
				menuDto.setMenuId(null==o[0]?null:Integer.parseInt(o[0].toString()));
				menuDto.setMenuName(null==o[1]?null:o[1].toString());
				menuDto.setMenuOrder(null==o[2]?null:Integer.parseInt(o[2].toString()));
				menuDto.setParentId(null==o[3]?null:Integer.parseInt(o[3].toString()));
				menuDto.setMenuUrl(null==o[4]?null:o[4].toString());
				menuList.add(menuDto);
			}
		}
		return menuList;  
	}
	
	/**
	 * 餐厅人员
	 * @param userId
	 * @return
	 * @throws DaoLevelException
	 */
	public List<MenuDto> RestaurentfindMenuTree(Integer userId) throws DaoLevelException {
		String sql = "select distinct dm.menu_id,dm.menu_name,dm.menu_order,dm.parent_id,dm.menu_url" +
				" from rk_restaurent_user u,rk_role r,rk_restaurant_admin_role ur,rk_dic_menu dm,rk_role_menu rm" +
						" where u.id=ur.user_id" 
						+" and r.role_id=ur.role_id"
						+" and dm.menu_id = rm.menu_id"
						+" and r.role_id = rm.role_id"
						+" and u.id="+ userId;
		List list = super.exeSQLList(sql);
		List<MenuDto> menuList =  new ArrayList<MenuDto>();
		MenuDto menuDto = null;
		if (list != null && list.size() > 0) {
			for (int i = 0;i < list.size(); i++) {
				Object [] o = (Object[])list.get(i);
				menuDto = new MenuDto();
				menuDto.setMenuId(null==o[0]?null:Integer.parseInt(o[0].toString()));
				menuDto.setMenuName(null==o[1]?null:o[1].toString());
				menuDto.setMenuOrder(null==o[2]?null:Integer.parseInt(o[2].toString()));
				menuDto.setParentId(null==o[3]?null:Integer.parseInt(o[3].toString()));
				menuDto.setMenuUrl(null==o[4]?null:o[4].toString());
				menuList.add(menuDto);
			}
		}
		return menuList;  
	}
}
